package com.codeshellme.bookmanager.controller;

import com.codeshellme.bookmanager.dto.BookAuthor;
import com.codeshellme.bookmanager.dto.BookItem;
import com.codeshellme.bookmanager.service.SysService;
import com.codeshellme.bookmanager.utils.CommonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SysController {

    @Autowired
    private SysService sysService;

    @RequestMapping("/sys_dict")
    public String role() {
        return "sys_dict";
    }

    @RequestMapping("/sys_author")
    public String sys_author() {
        return "sys_author";
    }

    @RequestMapping("/sys_book")
    public String sys_book() {
        return "sys_book";
    }

    // 分页查询字典信息
    @ResponseBody
    @RequestMapping("/querySysDict")
    public CommonResult querySysDict(Integer dict_id, Integer father_id, Integer page, Integer rows) {

        PageInfo<Map<String, Object>> pageInfo = sysService.querySysDict(dict_id, father_id, page, rows);

        // 构建 EasyUI 分页需要的数据
        Map<String, Object> ret = new HashMap<>();
        ret.put("rows", pageInfo.getList());
        ret.put("total", pageInfo.getTotal());

        return CommonResult.success(ret);
    }

    // 分页查询作者信息
    @ResponseBody
    @RequestMapping("/queryAllAuthors")
    public CommonResult queryAllAuthors(String name, Byte sex, Integer nationality_id, Byte status,
                                        String date_start, String date_end,
                                        Integer page, Integer rows) {

        PageInfo<Map<String, Object>> pageInfo = sysService.queryAllAuthors(
                name, sex, nationality_id, status, date_start, date_end, page, rows);

        // 构建 EasyUI 分页需要的数据
        Map<String, Object> ret = new HashMap<>();
        ret.put("rows", pageInfo.getList());
        ret.put("total", pageInfo.getTotal());

        return CommonResult.success(ret);
    }

    // 查询国籍
    @ResponseBody
    @RequestMapping("/queryAllNations")
    public CommonResult queryAllNations(String q) {
        List<Map<String, Object>> nations = sysService.queryAllNations(q);
        return CommonResult.success(nations);
    }

    @ResponseBody
    @RequestMapping("/updateAuthor")
    public CommonResult updateAuthor(BookAuthor author) {
        int ret = sysService.updateAuthor(author);
        if (ret < 0) {
            return CommonResult.failed("更新作者失败");
        } else {
            return CommonResult.success("更新作者成功");
        }
    }

    @ResponseBody
    @RequestMapping("/addAuthor")
    public CommonResult addAuthor(BookAuthor author) {
        int ret = sysService.addAuthor(author);
        if (ret < 0) {
            return CommonResult.failed("新增作者失败");
        } else {
            return CommonResult.success("新增作者成功");
        }
    }

    @ResponseBody
    @RequestMapping("/deleteAuthorById")
    public CommonResult deleteAuthorById(@RequestParam(value = "id") Integer authorId) {
        int ret = sysService.deleteAuthorById(authorId);
        if (ret != 1) {
            return CommonResult.failed("删除作者失败");
        } else {
            return CommonResult.success("删除作者成功");
        }
    }

    // 分页查询图书条目
    @ResponseBody
    @RequestMapping("/queryAllSysBooks")
    public CommonResult queryAllSysBooks(String name, Integer type_id, Integer author_id, Byte status,
                                         String date_start, String date_end, String q,
                                         Integer page, Integer rows) {

        PageInfo<Map<String, Object>> pageInfo = sysService.queryAllSysBooks(
                name, type_id, author_id, status, date_start, date_end, q, page, rows);

        // 构建 EasyUI 分页需要的数据
        Map<String, Object> ret = new HashMap<>();
        ret.put("rows", pageInfo.getList());
        ret.put("total", pageInfo.getTotal());

        return CommonResult.success(ret);
    }

    // 查询图书类型
    @ResponseBody
    @RequestMapping("/queryBookTypes")
    public CommonResult queryBookTypes(String q) {
        List<Map<String, Object>> types = sysService.queryBookTypes(q);
        return CommonResult.success(types);
    }

    // 查询图书作者
    @ResponseBody
    @RequestMapping("/queryBookAuthors")
    public CommonResult queryBookAuthors(String q) {
        List<Map<String, Object>> authors = sysService.queryBookAuthors(q);
        return CommonResult.success(authors);
    }

    @ResponseBody
    @RequestMapping("/updateBookItem")
    public CommonResult updateBookItem(BookItem book) {
        int ret = sysService.updateBookItem(book);
        if (ret < 0) {
            return CommonResult.failed("更新作者失败");
        } else {
            return CommonResult.success("更新作者成功");
        }
    }

    @ResponseBody
    @RequestMapping("/addBookItem")
    public CommonResult addBookItem(BookItem book) {
        int ret = sysService.addBookItem(book);
        if (ret < 0) {
            return CommonResult.failed("新增图书失败");
        } else {
            return CommonResult.success("新增图书成功");
        }
    }


    @ResponseBody
    @RequestMapping("/deleteBookItemById")
    public CommonResult deleteBookItemById(@RequestParam(value = "id") Integer bookId) {
        int ret = sysService.deleteBookItemById(bookId);
        if (ret != 1) {
            return CommonResult.failed("删除条目失败");
        } else {
            return CommonResult.success("删除条目成功");
        }
    }

    @ResponseBody
    @RequestMapping("/verifyBookItemName")
    public Boolean verifyBookItemName(String name) {
        return sysService.verifyBookItemName(name);
    }
}
